/*
 * @Author: your name
 * @Date: 2021-04-07 15:07:38
 * @LastEditTime: 2021-04-22 23:34:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\pushMod\service.go
 */
package pushMod

import "chronic/common"

func addOnePush(push pushDto) (*Push, error) {
	db := common.UseDb()
	p := Push{Disease: push.Disease, Sort: push.Sort, Cover: push.Cover, Title: push.Title, Picture1: push.Picture1, Text1: push.Text1, Picture2: push.Picture2, Text2: push.Text2, Text3: push.Text3, AuthorID: push.AuthorID}
	err := db.Save(&p).Error
	return &p, err
}

func findPushesByDisease(disease string) ([]*Push, error) {
	db := common.UseDb()
	var pushes []*Push
	err := db.Where("disease=?", disease).Preload("Author").Order("created_at desc").Find(&pushes).Error
	return pushes, err
}

func findAllPushes() ([]*Push, error) {
	db := common.UseDb()
	var pushes []*Push
	err := db.Order("created_at desc").Find(&pushes).Error
	return pushes, err
}

func deletePush(id uint) error {
	db := common.UseDb()
	err := db.Where("id = ?", id).Delete(&Push{}).Error
	return err
}

func findPushesByAuthorId(id uint) ([]*Push, error) {
	db := common.UseDb()
	var pushes []*Push
	err := db.Where("author_id = ?", id).Order("created_at desc").Find(&pushes).Error
	return pushes, err
}

func findPushesBySort(condition string) ([]*Push, error) {
	db := common.UseDb()
	var pushes []*Push
	err := db.Where("disease = ?", condition).Or("sort = ?", condition).Order("created_at desc").Find(&pushes).Error
	return pushes, err
}
